<?php
/**
 * Created by PhpStorm.
 * User: liang
 * Date: 2017/2/24
 * Time: 16:02
 */
namespace App\Helpers;
use Illuminate\Http\Request;
use App\AdminModel\Archive;
use App\AdminModel\Arctype;
use App\AdminModel\Projects;
use App\AdminModel\Ad;
use App\AdminModel\Link2;
use App\AdminModel\ProjectsCategories;

class Common
{
    
    public static function cateCid()
    {
    	
    	$strname =  str_replace("http://","",substr(\Request::getUri(),0,strpos(\Request::getUri(),'.')));
        
    	if($strname != 'www' && $strname != 'wap' ){
    		// $cid = ProjectsCategories::where('name',$strname)->value('cid')?:abort(404);

    		if($strname=='canyin')
    			$cid = 0;
    		else{
    			$cid = Arctype::where('typedir',$strname)->value('cid');
               // dd($cid);
                if($cid !=0)
                {
                    if($cid > 0){

                    }
                    else    
                         return abort(404);
                }
            }
            
    	}else if($strname != 'www' && $strname == 'wap'){
   //  		dd(\Request::path());
			// $path = \Request::path();
			
    		if(\Request::path()=='canyin' || \Request::path()=='/')
    			$cid = 0;
    		else{
    		 	$cid = Arctype::where('typedir',\Request::path())->value('cid');
    		 	if($cid !=0)
    			{
    		 		if($cid > 0){

    		 		}
    				else	
						 return abort(404);
    			}

    		}

    	}
    	else
    		$cid = 0;

    	return $cid;

    }
    public static function types($num,$catid=0)
    {

    	if($num==1){
	    	return   cache()->remember('cateArr', 150, function() {
	            return ProjectsCategories::where('parent_cid',18)->orderBy('sort', 'asc')->get();
	        });

		}else if($num >1){

			$cidArr = cache()->remember('cidArr_catid'.$catid,150, function() use($catid){
                if($catid == 0)
                    $catid = 18;

                $parent_cid = ProjectsCategories::where('cid',$catid)->value('parent_cid');

                if($parent_cid!=0){
                    $ccid =  ProjectsCategories::select('cid')->where('cid',$catid)->orderBy('sort', 'asc')->get();
                }else{
                    $ccid =  ProjectsCategories::select('cid')->where('parent_cid',$catid)->orderBy('sort', 'asc')->get();
                }
                return $ccid;
	        });

            // dd($cidArr);

			if($num==2){
				return $newsPinpai10 = cache()->remember('newsPinpai10_'.$catid, 1, function() use ($cidArr,$catid){
		            	return Projects::whereIn('cid',$cidArr)->take(18)->where('index_status',1)->latest()->get();
		        });
			}else if($num==3){
				return cache()->remember('lyPinpai15_'.$catid, 150, function()  use ($cidArr,$catid){
		            return Projects::whereIn('cid',$cidArr)->take(18)->orderBy('comment_num','desc')->latest()->get();
		            
		        });
			}else if($num==4){
				return cache()->remember('hotPinpai48_'.$catid, 150, function()  use ($cidArr,$catid){
		            return Projects::whereIn('cid',$cidArr)->take(48)->orderBy('pv_week','desc')->latest()->get();
		        });
			}else if($num==5){

				return cache()->remember('hot_projects_catid_' . $num."_".$catid, 150, function () use ($cidArr,$catid) {
		           	 return Projects::whereNotNull('logo')->where('logo', '!=', '')->whereIn('cid',$cidArr)->orderBy('pv_num', 'desc')->take(10)->get();
		        }); 
			}
			else if($num==6){
				return cache()->remember('news_projects_catid_' . $num."_".$catid, 150, function () use ($cidArr,$catid) {
		            	return Projects::whereNotNull('logo')->where('logo', '!=', '')->whereIn('cid',$cidArr)->orderBy('created_at', 'desc')->take(10)->get();
		        }); 
			}
			else if($num==7){
				return cache()->remember('hotjm_projects_catid_' . $num."_".$catid, 150, function () use ($cidArr,$catid) {
		            return Projects::whereNotNull('logo')->where('logo', '!=', '')->whereIn('cid',$cidArr)->orderBy('pv_num', 'desc')->take(40)->get();
		        }); 
			}
			else if($num==8){
				return cache()->remember('newsjm_projects_catid_' . $num."_".$catid, 150, function () use ($cidArr,$catid) {
		            return Projects::whereNotNull('logo')->where('logo', '!=', '')->whereIn('cid',$cidArr)->orderBy('created_at', 'desc')->take(40)->get();
		            
		        }); 
			}else if($num==9){
                return cache()->remember('newsjm_projects_catid_' . $num."_".$catid, 150, function () use ($cidArr,$catid) {
                        return Projects::whereNotNull('logo')->where('logo', '!=', '')->whereIn('cid',$cidArr)->orderBy('created_at', 'desc')->take(40)->get();
                }); 
            }else if($num==10){
                return cache()->remember('hot_right_paihangbang_catid_' . $num."_".$catid, 150, function () use ($cidArr,$catid) {
                        return Projects::whereNotNull('logo')->where('logo', '!=', '')->whereIn('cid',$cidArr)->orderBy('pv_week', 'desc')->orderBy('pv_month', 'desc')->take(15)->get();
                }); 
            }
			
		}

        
    }

 


    public static function archives($str,$ordery ='id',$catid=0)
    {
        
        if($catid == 0)
            $pid = 0;
        else {
           $pid = cache()->remember('pidarchives_'.$catid, 1, function() use($catid){
               return Arctype::where('cid',$catid)->value('id');
            });
           
        }
        $arctopId = Arctype::where('topid',$pid);
    	if($str==0)
    	{
    		return cache()->remember('newsZx10_'.$catid, 1, function() use($ordery,$catid,$pid){
    			if($catid == 0){
                   $allnavinfos=Arctype::select('id')->where('is_write',1)->where('mid',0)->where('cid',0)->get();
	            	return Archive::where('pid',$pid)->whereIn('typeid',$allnavinfos)->take(10)->orderBy($ordery,'desc')->get();
                }
	            else
	            	return Archive::take(10)->where('pid',$pid)->orderBy($ordery,'desc')->get();
	        });
    	}
    	elseif($str==1)
    	{
           
    		return cache()->remember('zhinanlitsts_'.$catid, 1, function() use($pid,$catid,$arctopId) {
                $tid = $arctopId->where('typedir','zhinan')->value('id');
                $arcives = Archive::where('pid',$pid);
                if($catid == 0)
                    return $arcives->where('typeid',1)->take(6)->latest()->get();
                else    
	            	return $arcives->where('typeid',$tid)->take(6)->latest()->get();
	        });
    	}
    	elseif($str==2)
    	{
    		return cache()->remember('geiyonglists_'.$catid, 1, function() use($pid,$catid,$arctopId) {
	            $tid = $arctopId->where('typedir','feiyong')->value('id');
                $arcives = Archive::where('pid',$pid);
                if($catid == 0)
                    return $arcives->where('typeid',2)->take(6)->latest()->get();
                else    
                    return $arcives->where('typeid',$tid)->take(6)->latest()->get();

	        });
    	}
    	elseif($str==3)
    	{
    		return cache()->remember('shangjilists_'.$catid, 1, function()  use($pid,$catid,$arctopId) {
	            $tid = $arctopId->where('typedir','shangji')->value('id');
                $arcives = Archive::where('pid',$pid);
                if($catid == 0)
                    return $arcives->where('typeid',3)->take(6)->latest()->get();
                else    
                    return $arcives->where('typeid',$tid)->take(6)->latest()->get();

	        });
    	}
    	elseif($str==4)
    	{
    		return cache()->remember('anlilitsts_'.$catid, 1, function()  use($pid,$catid,$arctopId) {
	            $tid = $arctopId->where('typedir','anli')->value('id');
                $arcives = Archive::where('pid',$pid);
                if($catid == 0)
                    return $arcives->where('typeid',4)->take(6)->latest()->get();
                else    
                    return $arcives->where('typeid',$tid)->take(6)->latest()->get();
	        });
    	}
    } 

    public static function ad($block, $part, $catid = 0)
    {
        return cache()->remember('link_ad_' . $block . '_' . $part . '_' . $catid, 150, function () use ($block, $part, $catid) {
            return Ad::where('block', $block)->where('part', $part)->where('project_cid', $catid)->orderBy('sort', 'asc')->get();
        });
    }

    public static function link2($cid=0, $sort=0)
    {
		return cache()->remember('link2_ad_' . $cid . '_'. $sort, 1, function () use ($cid,$sort) {
            if($cid==0)
    	           $cid = 18;
                return Link2::where('status', 1)->where('project_cid', $cid)->where('sort', $sort)->orderBy('id', 'desc')->get();
            
        });
    	
    }

    public static function arctypes($catid = 0)
    {

        return cache()->remember('arctypes_'.$catid, 1, function() use($catid){
            if($catid == 0)
                return Arctype::where('topid', 0)->where('cid', 0)->where('mid', 0)->orderBy('sortrank', 'asc')->get();
            else
                return Arctype::where('topid', $catid)->where('cid', 0)->where('mid', 0)->orderBy('sortrank', 'asc')->get();
        });
    }

    public static function seoarctype($cid)
    {
    	if($cid==0)
    		$cid = 18;
        return cache()->remember('seoarctype_'.$cid, 1, function() use($cid) {
           return Arctype::where('cid', $cid)->first();
        });
    }

    public static function headArctype($typedir)
    {
        return cache()->remember('headArctype'.$typedir, 1, function() use($typedir) {
           return Arctype::where('typedir', $typedir)->value('typename');
        });
    }


   public static  function invest($level)
{

	$level_cn = '';
    if($level == 9)
    	$level_cn = '未知';
    else if($level == 1)
    	$level_cn = '0万';
    else if($level == 2)
    	$level_cn = '0-1万';
    else if($level == 3)
    	$level_cn = '1-5万';
    else if($level == 4)
    	$level_cn = '5-10万';
    else if($level == 5)
    	$level_cn = '10-20万';
    else if($level == 6)
    	$level_cn = '20-50万';
    else if($level == 7)
    	$level_cn = '50-100万';
    else if($level == 8)
    	$level_cn = '100万以上';
   
    return $level_cn;
    
}


    //留言
public static function liuyan()
{
    $provinces = [
        '湖南', '贵州', '广东', '河南', '河北', '山西', '浙江', '福建', '上海', '北京', '湖北', '江西', '广西', '黑龙江', '吉林',
    ];
    $telsH = [131, 134, 135, 136, 137, 138, 139, 147, 150, 152, 156, 170, 177, 180, 181, 186, 189];
    $sexs = ['先生', '女士'];
    $image = ['https://www.u88.com/public/images/portrait_boy.jpg','https://www.u88.com/public/images/portrait_girl.jpg'];
    $surnames = ['赵', '孙', '李', '周', '吴', '邓', '戴', '郭', '王', '易', '唐', '张', '刘'];
    $msgs = [
        '你好！我想加盟代理你们的品牌，请联系我。',
        '对该项目产生意向：我想知道加盟费用是多少。谢谢',
        '你们的总部在哪里部在哪里？',
        '我想加盟，请联系我。',
        '初步打算加入贵公司，请寄资料。',
        '你好！我想加盟代理你们的品牌，请联系我。',
        '请问店面面积需要多大？',
        '做为代理加盟商可以得到哪些支持?',
        '你们的产品经营方向是？',
        '你们用户的人群定位和发展规划是怎样的？',
        '加盟费用和初期投资多少，一般多久能收回成本？',
        '开店有什么支持？',
        '很想合作，来电话细谈吧。',
        '请问具体加盟流程是怎么样的?',
        '请问贵公司哪里有样板店或直营店？',
        '请给我打电话，并寄送加盟资料。',
        '你们的总部再哪里？',
        '请问店面面积需要多大？',
        '我想知道加盟费是多少，谢谢！',
        '请问具体加盟流程是怎么样的?',
        '请问贵公司哪里有样板店或直营店？',
        '很想合作，来电话细谈吧。',
        '我想知道加盟费是多少，谢谢！',
        '这边有几家加盟店再哪个位置？',
        '我这里是乡镇，可以加盟吗',
        '我想加盟，要怎么办？',
        '我想加盟，需要什么条件？',
        '想加盟，请您告知具体事宜！',
        '我想加盟 请尽快联系我',
        '有兴趣了解加盟项目。',
        '我想加盟，看到电话请联系我',
        '代理咨询和加盟条件',
        '怎样的加盟方式。是不是公司选址。费用怎么算',
        '我想加盟，感觉不错',
        '我想咨询关于加盟的事议',
        '咨询一下加盟条件流程',
        '加盟流程，加盟条件',
        '我想加盟，具体细节，及费用事项。',
        '我想加盟专卖请问有联系方式吗',
        '您好 我想了解一下罗森的加盟都需要什么条件 和加盟流程等',
        '怎么加盟 有没有加盟费',
        '我想加盟请跟我联系',
        '需要了解加盟全部资金',
        '我想加盟贵公司',
        '我想加盟 想了解一些相关事宜',
        '加盟程序以及条件，或者是否有可能深化合作。',
        '我们这个地方贵司有业务吗？我想加盟能支持不？谢谢！',
        '很有想法，但不知如何去装修、设计',

    ];
    $liuyan = [];
    $start = time() - 28800;
    for ($i = 0; $i < 5; $i++) {
        $msg = collect();
        $msg->province = $provinces[rand(0, count($provinces) - 1)];
        $sex = rand(0, 1);
        $msg->image = $image[$sex];
        $msg->name = $surnames[rand(0, count($surnames) - 1)] . $sexs[$sex];
        $msg->msg = $msgs[rand(0, count($msgs) - 1)];
        $msg->time = date('m-d h:i', rand($start, $start + 2160));
        $msg->phone = $telsH[rand(0, count($telsH) - 1)] . '******' . rand(10, 99);
        $start += 1600;
        $liuyan[] = $msg;
    }

    return $liuyan;
}

public static function arctypeTwo()
    {

        return Arctype::where('topid', 0)->where('cid','!=', 0)->where('mid', 0)->orderBy('sortrank', 'asc')->get();
        
    }


}